<?php


namespace app\websocket\model;


use think\Model;

class ActionRole extends Model
{
    public static function checkUserActionRole($id,$action)
    {
        $UserClass=new User();

/*        if(!is_array($action)){
            $action=[$action];
        }*/

        $role=$UserClass->alias('u')
            ->join('action_role ar','u.id=ar.user_id or u.role=ar.role_id or u.role_detail_id=ar.role_detail_id or u.group_id=ar.group_id','left')
            ->join('__ACTION__ a','ar.action_id=a.id','left')
            ->where('u.id',$id)
            ->where('a.action_name','=',$action)
            ->where('u.status',1)
            ->where('u.del_flag',0)
            ->where('ar.del_flag',0)
            ->where('a.del_flag',0)
            ->field('a.action_name,max(ar.action_type) action_type')
            ->group('a.action_name')
            ->select();
        $action_arr=$role->toArray();
        if(empty($action_arr)){
            return false;
        }

        $group_id=$UserClass->where('id',$id)->value('group_id');

        $action_type=$action_arr[0]['action_type'];
/*        foreach ($action_arr as $key=>$value){
            if($value['action_name']===$action){
                $action_type=$value['action_type'];
            }
        }*/

        if($action_type===1) {
            $search_user = null;
            $group=$group_id;
        }else if($action_type===2){
            $search_user=null;
            $group=$group_id;
        }else{
            $search_user = $id;
            $group=$group_id;
        }

        return ['search_user'=>$search_user,'group'=>$group];
    }
}